/*
 * Copyright 2003-2007 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

package com.colony.concurrent;

/**
 *  List is a basic linked list designed to act as a Queue.  Single direction
 *  traversal only.
 */
public class List
{

   private ListItem _front;
   private ListItem _back;
   private int _count;


   List()
   {
      _front = null;
      _back = null;
      _count = 0;
   }


   /**
    *  returns true if the List is empty.
    */

   boolean isEmpty()
   {
      synchronized( this )
      {
         if ( _front == null ) return true;
         return false;
      }
   }

   /**
    *  Add an object to the list.
    */

   void add( Object o )
   {
      synchronized( this )
      {
         ListItem l = new ListItem( o );

         if ( isEmpty() )
         {
            _front = l;
            _back = l;
         }
         else
         {
            _back.next = l;
            _back = l;
         }
         _count++;
      }
   }

   /**
    *  Remove an Object from the list using FIFO method.  Returns null is list is empty.
    */

   Object remove()
   {
      synchronized( this )
      {
         if ( isEmpty() == true )
            return null;
  
         ListItem l = _front;
         _front = _front.next;
         _count--;
         return l.item;
      }
   }     


   public int count()
   {
      synchronized( this )
      {
          return _count;
      }      
   }

   private class ListItem
   {
      public Object item;
      public ListItem next;

      ListItem( Object o )
      {
         item = o;
      }
   }

}
